package com.googlecode.bipol.war.session;

import java.io.Serializable;
import org.apache.log4j.Logger;
import org.charless.spring.jsonrpc.JsonRpcController;

import com.googlecode.bipol.war.services.QuestionManager;
import com.googlecode.bipol.war.services.QuestionManagerImpl;
import com.googlecode.bipol.war.services.TopicManager;
import com.googlecode.bipol.war.services.TopicManagerImpl;
import com.metaparadigm.jsonrpc.JSONRPCBridge;

/**
 * TODO: Doc
 *
 */
public class BipSession
    extends JsonRpcController
        implements IBipSession, Serializable
{
    private static final long serialVersionUID = 1L;
    private static final transient Logger logger = Logger.getLogger( BipSession.class );
    // example of extension (?)
    //private Set<String> registerObjectSet = new HashSet<String>();

    /** current user name */
    private String userName;

    public BipSession()
    {
        super();
    }

    /**
     * TODO: Doc
     */
    protected void registerSessionAjaxServicesImpl( JSONRPCBridge bridge ) throws Exception
    {
        bridge.enableReferences();
        //bridge.registerCallableReference(.class);

        this.userName = getHttpServletRequest().getRemoteUser();

        logger.info("JSON Session bridge '"+bridge.toString()+"'");
        logger.info(this.toString());
        logger.info("UserName="+this.userName);

// old method
        //bridge.registerObject("TopicManager", new TopicManagerImpl(),com.googlecode.bipol.war.services.TopicManager.class);
        //bridge.registerObject("QuestionManager", new QuestionManagerImpl(),com.googlecode.bipol.war.services.QuestionManager.class);

// new method usage *examples* ! (check type + auto named)
        registerObject(bridge, "TopicManager", new TopicManagerImpl(), TopicManager.class );
        // error -> error at compilation time : registerObject(bridge, "TopicManager", new TopicManagerImpl(), QuestionManager.class );
        registerObject(bridge, new QuestionManagerImpl(),QuestionManager.class);

        logger.info("registerSessionAjaxServicesImpl() done");
    }

    private <T> void registerObject(
            JSONRPCBridge   bridge,
            String          name,
            T               object,
            Class<T>        interfaceClass
            )
    {
//        if( registerObjectSet.contains( name ) ) {
//            logger.warn( "Object already registred: " + name );
//            // throw something ???
//            }

        bridge.registerObject( name, object, interfaceClass );
//        registerObjectSet.add( name );
    }

    private <T> void registerObject(
            JSONRPCBridge   bridge,
            T               object,
            Class<T>        interfaceClass
            )
    {
        bridge.registerObject( interfaceClass.getCanonicalName(), object, interfaceClass );
    }

//    /**
//     * For debugging propose ONLY
//     *
//     * @return an unmodifiable Set of registered object names
//     */
//    public Set<String> getRegistredObjectNamesOnJSONRPCBridge()
//    {
//        return Collections.unmodifiableSet( registerObjectSet );
//    }

    /* (non-Javadoc)
     * @see com.googlecode.bipol.war.session.IBipolSession#getUsername()
     */
    @Override
    public String getUsername()
    {
        return this.userName;
    }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append( super.toString() )
               .append( " [" )
               .append( "userName=" )
               .append( userName )
               .append( "]" );
        return builder.toString();
    }
}

